package FlowGraph;

import java.util.*;

//////////////////////////////////////////////////////////////////////////
//// DominatorTreeNode
/**
   Um no da arvore de immediate dominators.

@author Ivan Jeukens
@version v 0.1 12/11/2002
*/
public class DominatorTreeNode {

    public DominatorTreeNode(FlowGraphNode container) {
        _container = container;
        container.setDominatorTreeNode(this);
        _children = new LinkedList();

        _frontier = new LinkedList();
        _dominates = new LinkedList();
    }

    ///////////////////////////////////////////////////////////////////
    ////                        public methods                     ////

    public void setParent(DominatorTreeNode node) {
        _parent = node;
    }

    public DominatorTreeNode getParent() {
        return _parent;
    }

    public void addChild(DominatorTreeNode node) {
        _children.add(node);
    }

    public Iterator childrenIterator() {
        return _children.iterator();
    }
    
    public int childrenSize() {
        return _children.size();
    }
    
    public FlowGraphNode getContainer() {
        return _container;
    }

    // O no node tem que estar em um nivel mais baixo
    public boolean dominates(DominatorTreeNode node) {
        while(node != null) {
            if(node == this) return true;
            node = node.getParent();
        }
        return false;
    }
    
    public void removeNode(DominatorTreeNode node) {
        _children.remove(node);
    }
    
    public String toString() {
        StringBuffer buf = new StringBuffer("Node " + _container.toString() + 
            " immediate dominates: ");
        for(int i = 0;i < _children.size();i++) {
            DominatorTreeNode n = (DominatorTreeNode) _children.get(i);
            buf.append(n.getContainer().toString() + " ");
        }
        return buf.toString();
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                        private variables                  ////

    private FlowGraphNode _container;

    private DominatorTreeNode _parent;
    private LinkedList _children;

    private LinkedList _frontier;
    private LinkedList _dominates;
}
